package com.ly.mpayment.persist.service.yidong;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.SQLQuery;
import org.springframework.stereotype.Service;

import com.ly.mpayment.core.util.DateUtils;
import com.ly.mpayment.core.util.ListRange;
import com.ly.mpayment.persist.entity.yidong.NumberPool;
import com.ly.mpayment.persist.hibernate.Page;
import com.ly.mpayment.persist.service.AbstractService;

import cn.jpush.api.utils.StringUtils;
@Service
public class NumberPoolServiceImpl extends AbstractService<NumberPool, Serializable> implements INumberPoolService{

	@Override
	public Page<NumberPool> findPageNumberPool(Map<String, Object> parameters, int start, int pageSize) {
//		return this.findPageByNamed("select b from NumberPool b where b.status = 1 #[and b.busname like :busname ]#  #[and b.busCode like :busCode ]# order by b.busname", parameters, start, pageSize);
		 
		return this.findPageByNamed("select n from NumberPool n where 1 = 1 #[ and n.mobilePhone = :mobilePhone]# #[ and n.isRecharge = :isRecharge]# "
				+ " #[ and n.state = :state]# #[ and n.isUsed = :isUsed]#  order by n.loginTime,n.renovateTime desc", parameters, start, pageSize);
	}

	@Override
	public boolean saveNumberPool(NumberPool pool) {
		if(pool != null){
			this.saveOrUpdate(pool);
			return true;
		}
		return false;
	}

	@Override
	public boolean updateNumberPool(NumberPool pool) {
		if(pool != null){
			this.updateEntity(pool);
			this.getSession().flush();
			return true;
		}
		return false;
	}

	@Override
	public NumberPool findByPhone(String phone) {
		return this.findUnique("select n from NumberPool n where n.mobilePhone = ? ", phone);
	}

	@Override
	public NumberPool findById(String id) {
		return this.findUnique("select n from NumberPool n where n.numberPoolId = ? ", id);
	}

	@Override
	public Page<NumberPool> findLoginPool(int start, int pageSize) {
		return this.findPageByNamed("select n from NumberPool n where n.state = 1 and n.isRecharge = 1  order by n.renovateTime ", new HashMap<>(), start, pageSize);
	}

	@Override
	public long findLoginPoolCount() {
		return this.findLong("select count(*) from NumberPool n where n.state = 1 and n.isRecharge = 1  order by n.renovateTime", new HashMap<>());
	}

	@SuppressWarnings("unchecked")
	@Override
	public NumberPool findUsePool() {
//		SQLQuery query = this.getSession().createSQLQuery("SELECT * FROM number_pool n where n.state = 1 and n.is_recharge = 1 and n.is_used = 0 and n.current_num < 10 order by  n.current_num desc ,n.renovate_time limit 1 ");
//		List<NumberPool> lists = query.list();
//		if(lists != null && lists.size() > 0){
//			Object obj [] = (Object) lists.get(0);
//			return lists.get(0).toString();
//		}
//		return null;
		
		Page<NumberPool> pages = this.findPageByNamed("SELECT n FROM NumberPool n where n.state = 1 and n.isRecharge = 1 and n.isUsed = 0 and n.currentNum < 10 order by  n.currentNum desc,n.renovateTime", new HashMap<>(), 0, 1);
		if(pages != null ){
			List<NumberPool> lists = pages.getResult();
			if(lists != null &&lists.size() > 0){
				return lists.get(0);
			}
		}
		return null;
	}

	@Override
	public long findUsedPoolCount() {
		return this.findLong("select count(*) from NumberPool n where n.currentNum >= 10 order by n.renovateTime", new HashMap<>());
	}

	@Override
	public Page<NumberPool> findUsedPoolCount(int start, int pageSize) {
		return this.findPageByNamed("select n from NumberPool n where n.currentNum >= 10 and n.isUsed = 1 order by n.renovateTime ", new HashMap<>(), start, pageSize);
	}

	@Override
	public long findIsUse() {
		return this.findLong("SELECT count(*) FROM NumberPool n where n.state = 1 and n.isRecharge = 1 and n.currentNum < 10 order by n.renovateTime , n.currentNum desc ");
	}

	/**
	 * 查询号码在当月是否已经充值使用
	 */
	@Override
	public NumberPool findByPhoneMonth(String phone) {
		// TODO Auto-generated method stub
		String time = DateUtils.convertYearMonth(DateUtils.getNowDate(), DateUtils.yearMonth);
//		select * from number_pool n where n.mobile_phone = '' and DATE_FORMAT(n.login_time,'%Y%m') ='201907'
		return this.findOneObject("select n from NumberPool n where n.mobilePhone = ? and DATE_FORMAT(n.loginTime,'%Y%m') = ? ", phone,time);
	}
//
//	@Override
//	public Page<NumberPool> findNotUsed(int start, int pageSize) {
//		 return this.findPageByNamed("SELECT n FROM NumberPool n where n.state = 0 and n.isRecharge = 0 and n.isUsed = 0 and n.currentNum  = 0 and n.smsCode = '' order by  n.loginTime ", new HashMap<>(), start, pageSize);
//	}

	@Override
	public NumberPool findUsePoolDaoxu(int num) {
		Page<NumberPool> pages = this.findPageByNamed("SELECT n FROM NumberPool n where n.state = 1 and n.isRecharge = 1 and n.isUsed = 0 and n.currentNum < 10 order by  n.currentNum desc,n.renovateTime desc", new HashMap<>(), 0, 20);
		if(pages != null ){
			List<NumberPool> lists = pages.getResult();
			if(lists != null &&lists.size() > 0){
				return lists.get(num);
			}
		}
		return null;
	}

}
